<template>
  <div class="register-container">
    <div class="register-background">
      <div class="register-content">
        <div class="register-header">
          <div class="logo-section">
            <el-icon size="48" color="#2e7d32"><School /></el-icon>
            <h1>中山大学研究生院</h1>
            <p>校企对接平台 - 用户注册</p>
          </div>
        </div>

        <el-card class="register-card" shadow="never">
          <template #header>
            <div class="card-header">
              <h2>创建新账户</h2>
              <p>请填写以下信息完成注册</p>
            </div>
          </template>

          <el-form ref="formRef" :model="form" :rules="rules" label-width="100px" size="large">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="用户名" prop="username">
                  <el-input
                    v-model="form.username"
                    placeholder="请输入用户名"
                    prefix-icon="User"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="用户角色" prop="role">
                  <el-select v-model="form.role" placeholder="请选择用户角色" style="width: 100%">
                    <el-option label="企业用户" value="enterprise">
                      <div class="role-option">
                        <el-icon color="#2e7d32"><OfficeBuilding /></el-icon>
                        <span>企业用户</span>
                      </div>
                    </el-option>
                    <el-option label="导师用户" value="supervisor">
                      <div class="role-option">
                        <el-icon color="#4caf50"><User /></el-icon>
                        <span>导师用户</span>
                      </div>
                    </el-option>
                    <el-option label="学生用户" value="student">
                      <div class="role-option">
                        <el-icon color="#66bb6a"><User /></el-icon>
                        <span>学生用户</span>
                      </div>
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="真实姓名" prop="realName">
                  <el-input
                    v-model="form.realName"
                    placeholder="请输入真实姓名"
                    prefix-icon="User"
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="手机号码" prop="phone">
                  <el-input
                    v-model="form.phone"
                    placeholder="请输入手机号码"
                    prefix-icon="Phone"
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-form-item label="邮箱地址" prop="email">
              <el-input
                v-model="form.email"
                placeholder="请输入邮箱地址"
                prefix-icon="Message"
                clearable
              />
            </el-form-item>

            <el-form-item v-if="form.role === 'enterprise'" label="企业名称" prop="organization">
              <el-input
                v-model="form.organization"
                placeholder="请输入企业名称"
                prefix-icon="OfficeBuilding"
                clearable
              />
            </el-form-item>

            <el-form-item
              v-if="form.role === 'supervisor' || form.role === 'student'"
              label="所属学院"
              prop="department"
            >
              <el-select v-model="form.department" placeholder="请选择所属学院" style="width: 100%">
                <el-option label="计算机科学与技术学院" value="计算机科学与技术学院" />
                <el-option label="电子与信息工程学院" value="电子与信息工程学院" />
                <el-option label="材料科学与工程学院" value="材料科学与工程学院" />
                <el-option label="生物医学工程学院" value="生物医学工程学院" />
                <el-option label="环境科学与工程学院" value="环境科学与工程学院" />
                <el-option label="化学学院" value="化学学院" />
                <el-option label="物理学院" value="物理学院" />
                <el-option label="数学学院" value="数学学院" />
              </el-select>
            </el-form-item>

            <el-form-item
              v-if="form.role === 'supervisor' || form.role === 'student'"
              label="研究方向"
              prop="researchDirection"
            >
              <el-select
                v-model="form.researchDirection"
                multiple
                placeholder="请选择研究方向"
                style="width: 100%"
                filterable
              >
                <el-option label="人工智能" value="人工智能" />
                <el-option label="机器学习" value="机器学习" />
                <el-option label="深度学习" value="深度学习" />
                <el-option label="计算机视觉" value="计算机视觉" />
                <el-option label="自然语言处理" value="自然语言处理" />
                <el-option label="新材料" value="新材料" />
                <el-option label="纳米材料" value="纳米材料" />
                <el-option label="生物材料" value="生物材料" />
                <el-option label="网络安全" value="网络安全" />
                <el-option label="信息安全" value="信息安全" />
                <el-option label="数据安全" value="数据安全" />
                <el-option label="生物医学" value="生物医学" />
                <el-option label="生物技术" value="生物技术" />
                <el-option label="环境科学" value="环境科学" />
                <el-option label="电子工程" value="电子工程" />
                <el-option label="通信工程" value="通信工程" />
              </el-select>
            </el-form-item>

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="密码" prop="password">
                  <el-input
                    v-model="form.password"
                    type="password"
                    placeholder="请输入密码"
                    prefix-icon="Lock"
                    show-password
                    clearable
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="确认密码" prop="confirmPassword">
                  <el-input
                    v-model="form.confirmPassword"
                    type="password"
                    placeholder="请再次输入密码"
                    prefix-icon="Lock"
                    show-password
                    clearable
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-form-item>
              <el-checkbox v-model="form.agreeTerms">
                我已阅读并同意
                <el-button type="text" @click="showTerms">《用户协议》</el-button>
                和
                <el-button type="text" @click="showPrivacy">《隐私政策》</el-button>
              </el-checkbox>
            </el-form-item>

            <el-form-item>
              <el-button
                type="primary"
                :loading="loading"
                @click="handleSubmit"
                style="width: 100%"
              >
                立即注册
              </el-button>
            </el-form-item>
          </el-form>

          <div class="form-footer">
            <span>已有账户？</span>
            <el-button type="text" @click="goToLogin">立即登录</el-button>
          </div>
        </el-card>
      </div>
    </div>

    <!-- 用户协议对话框 -->
    <el-dialog v-model="termsVisible" title="用户协议" width="600px">
      <div class="terms-content">
        <h3>中山大学研究生院校企对接平台用户协议</h3>
        <p>欢迎使用中山大学研究生院校企对接平台！</p>
        <p>本协议是您与中山大学研究生院之间关于使用校企对接平台服务所订立的协议。</p>
        <h4>一、服务内容</h4>
        <p>平台为企业、导师、学生提供课题发布、申请、匹配等服务。</p>
        <h4>二、用户责任</h4>
        <p>用户应提供真实、准确的信息，遵守相关法律法规。</p>
        <h4>三、知识产权</h4>
        <p>平台内容的知识产权归中山大学所有。</p>
      </div>
      <template #footer>
        <el-button @click="termsVisible = false">关闭</el-button>
      </template>
    </el-dialog>

    <!-- 隐私政策对话框 -->
    <el-dialog v-model="privacyVisible" title="隐私政策" width="600px">
      <div class="terms-content">
        <h3>中山大学研究生院校企对接平台隐私政策</h3>
        <p>我们重视您的隐私保护，本政策说明了我们如何收集、使用和保护您的个人信息。</p>
        <h4>一、信息收集</h4>
        <p>我们收集您主动提供的基本信息，如姓名、联系方式、研究方向等。</p>
        <h4>二、信息使用</h4>
        <p>收集的信息仅用于平台服务，不会向第三方泄露。</p>
        <h4>三、信息保护</h4>
        <p>我们采用安全措施保护您的个人信息安全。</p>
      </div>
      <template #footer>
        <el-button @click="privacyVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { School, User, OfficeBuilding } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const loading = ref(false)
const formRef = ref()
const termsVisible = ref(false)
const privacyVisible = ref(false)

const form = reactive({
  username: '',
  role: '',
  realName: '',
  phone: '',
  email: '',
  organization: '',
  department: '',
  researchDirection: [] as string[],
  password: '',
  confirmPassword: '',
  agreeTerms: false,
})

const rules = computed(() => {
  let baseRules = {
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' },
      { pattern: /^[a-zA-Z0-9_]+$/, message: '用户名只能包含字母、数字和下划线', trigger: 'blur' },
    ],
    role: [{ required: true, message: '请选择用户角色', trigger: 'change' }],
    realName: [{ required: true, message: '请输入真实姓名', trigger: 'blur' }],
    phone: [
      { required: true, message: '请输入手机号码', trigger: 'blur' },
      { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' },
    ],
    email: [
      { required: true, message: '请输入邮箱地址', trigger: 'blur' },
      { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' },
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' },
      {
        pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)/,
        message: '密码必须包含大小写字母和数字',
        trigger: 'blur',
      },
    ],
    confirmPassword: [
      { required: true, message: '请确认密码', trigger: 'blur' },
      {
        validator: (rule: any, value: string, callback: Function) => {
          if (value !== form.password) {
            callback(new Error('两次输入密码不一致'))
          } else {
            callback()
          }
        },
        trigger: 'blur',
      },
    ],
  }

  if (form.role === 'enterprise') {
    baseRules.organization = [{ required: true, message: '请输入企业名称', trigger: 'blur' }]
  }

  if (form.role === 'supervisor' || form.role === 'student') {
    baseRules.department = [{ required: true, message: '请选择所属学院', trigger: 'change' }]
    baseRules.researchDirection = [
      { required: true, message: '请选择研究方向', trigger: 'change' },
      { type: 'array', min: 1, message: '至少选择一个研究方向', trigger: 'change' },
    ]
  }

  return baseRules
})

const handleSubmit = async () => {
  if (!formRef.value) return

  if (!form.agreeTerms) {
    ElMessage.warning('请先同意用户协议和隐私政策')
    return
  }

  try {
    await formRef.value.validate()
    loading.value = true

    // 模拟注册过程
    await new Promise((resolve) => setTimeout(resolve, 1500))

    ElMessage.success('注册成功！请登录')
    router.push('/login')
  } catch (error) {
    console.error('表单验证失败:', error)
  } finally {
    loading.value = false
  }
}

const goToLogin = () => {
  router.push('/login')
}

const showTerms = () => {
  termsVisible.value = true
}

const showPrivacy = () => {
  privacyVisible.value = true
}
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f8f9fa 0%, #e8f5e8 100%);
}

.register-background {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.register-content {
  width: 100%;
  max-width: 1200px;
  display: flex;
  align-items: center;
  gap: 60px;
}

.register-header {
  flex: 1;
  text-align: center;
  color: var(--primary-color);
}

.logo-section h1 {
  font-size: 32px;
  font-weight: 700;
  margin: 20px 0 10px;
  color: var(--primary-color);
}

.logo-section p {
  font-size: 18px;
  color: var(--text-secondary);
  margin: 0;
}

.register-card {
  flex: 1;
  max-width: 600px;
  border-radius: 16px;
  border: 1px solid var(--border-color);
  box-shadow: 0 8px 32px rgba(46, 125, 50, 0.1);
}

.card-header {
  text-align: center;
}

.card-header h2 {
  font-size: 24px;
  font-weight: 600;
  color: var(--primary-color);
  margin: 0 0 8px;
}

.card-header p {
  font-size: 14px;
  color: var(--text-secondary);
  margin: 0;
}

.role-option {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
}

.role-option span {
  font-weight: 500;
}

.form-footer {
  text-align: center;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid var(--border-color);
  color: var(--text-secondary);
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input,
.el-select {
  border-radius: 8px;
}

.el-input:focus,
.el-select:focus {
  border-color: var(--primary-color);
}

.el-button {
  border-radius: 8px;
  font-weight: 500;
}

.el-button--primary {
  height: 48px;
  font-size: 16px;
}

.terms-content {
  max-height: 400px;
  overflow-y: auto;
  line-height: 1.6;
}

.terms-content h3 {
  color: var(--primary-color);
  margin-bottom: 16px;
}

.terms-content h4 {
  color: var(--text-primary);
  margin: 16px 0 8px;
}

.terms-content p {
  margin: 8px 0;
  color: var(--text-secondary);
}

@media (max-width: 768px) {
  .register-content {
    flex-direction: column;
    gap: 30px;
  }

  .register-header {
    order: 2;
  }

  .register-card {
    order: 1;
    max-width: 100%;
  }
}
</style>
